package com.xuyuan.reflect.clazz;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.xuyuan.reflect.TestCommonClass;
import org.junit.jupiter.api.Test;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.Date;

public class SpringReflectUtilsTest {

    @Test
    public void test0() throws ClassNotFoundException {
        Class<?> clazz1 = TestCommonClass.class;
        Class<?> clazz2 = Class.forName(" com.xuyuan.reflect.TestCommonClass");
        Class<?> clazz3 = new TestCommonClass().getClass();
    }


    @Test
    public void test() {
        // TODO 1-获取当前类的所有Method
        // Method[] declaredMethods = ReflectionUtils.getDeclaredMethods(Person.class);
        // System.out.println(declaredMethods.length);
        // for (Method method : declaredMethods) {
        //     System.out.println(method.getName());
        // }

        // TODO 2-获取当前类及父类父接口的所有Method
        // Method[] allDeclaredMethods = ReflectionUtils.getAllDeclaredMethods(Person.class);
        // System.out.println(allDeclaredMethods.length);
        // for (Method method : allDeclaredMethods) {
        //     System.out.println(method.getName());
        // }

        // TODO 3-
        Method[] uniqueDeclaredMethods = ReflectionUtils.getUniqueDeclaredMethods(Person.class);
        System.out.println(uniqueDeclaredMethods.length);
        for (Method method : uniqueDeclaredMethods) {
            System.out.println(method.getName());
        }
        // TODO
        // TODO
    }

    interface IAnimal {
        default void run2() {

        }
    }

    static class Person extends Human implements IAnimal {
        @JsonProperty(value = "myName", index = 4)
        private String name;
        @JsonProperty(value = "myAge", index = 3)
        private Integer age;

        @JsonProperty(value = "birthday", index = 2)
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private Date date;
        @JsonProperty(value = "myHeight", index = 1)
        private int height;

        public static String method1() {
            return "method1";
        }

        protected String method2() {
            return "method2";
        }

        private String method3(String name) {
            return name;
        }

        private String method3(String name, int age) {
            return name + age;
        }
    }

    static class Animal {
        private String action;
        private String sex;

        private void run0() {
        }
    }

    static class Human extends Animal {
        private String sex1;
        private String action1;

        private void run1() {
        }
    }
}

